今天狀況不好,
等一下又有事要忙,
先將程式碼貼出來,
明天再做講解.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:id="@+id/TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="@string/bmi_hello" />
        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/bmi_height" />
        <EditText
            android:id="@+id/height"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="numberDecimal" />
        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/bmi_weight" />
        <EditText
            android:id="@+id/weight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="numberDecimal" />
        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/bmi_btn" />
        <TextView
            android:id="@+id/result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:textAppearanceLarge"/>
        <TextView
            android:id="@+id/suggest"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:textAppearanceLarge"/>
    </LinearLayout>
</android.support.constraint.ConstraintLayout>
MainActivity.java
package com.example.user.mybmi;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.LocaleList;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(calcBMI);
    }
    private View.OnClickListener calcBMI = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DecimalFormat nf = new DecimalFormat("0.00");
            EditText fieldheight = (EditText)findViewById(R.id.height);
            EditText fieldweight = (EditText)findViewById(R.id.weight);
            //身高
            double height = Double.parseDouble(fieldheight.getText().toString())/100;
            //體重
            double weight = Double.parseDouble(fieldweight.getText().toString());
            //計算出BMI值
            double BMI = weight / (height*height);
            //結果
            TextView result = (TextView)findViewById(R.id.result);
            result.setText(getText(R.string.bmi_result)
                    + nf.format(BMI));
            //建議
            TextView fieldsuggest = (TextView)findViewById(R.id.suggest);
            if(BMI > 25) //太重了
                fieldsuggest.setText(R.string.advice_heavy);
            else if(BMI < 20) //太輕了
                fieldsuggest.setText(R.string.advice_light);
            else //剛剛好
                fieldsuggest.setText(R.string.advice_average);
        }
    };
}
strings.xml
<resources>
    <string name="app_name">MyBMI</string>
    <string name="bmi_hello">哈囉!BMI</string>
    <string name="bmi_height">身高 (cm)</string>
    <string name="bmi_weight">體重 (kg)</string>
    <string name="bmi_btn">計算BMI值</string>
    <string name="bmi_result">你的BMI值是</string>
    <string name="advice_light">你該多吃點</string>
    <string name="advice_average">體型很棒喔!</string>
    <string name="advice_heavy">你該節食了</string>
</resources>
以下是執行結果
這個數值...絕對不是我!

想請問有很多函式庫是無法使用的/沒有使用到的
這在當初撰寫的時候是正常的嗎? 另外想請問有辦法偵測函式庫 並補足缺少的函式庫嗎?
1.沒用到的正常是可以刪, 不過因為算是測試專案, 我每天可能會用到不同的函式庫, 就沒特別去做刪的動作了.
2.Android Studio會自己偵測要載入的函式庫, 如果有偵測到的話, 這部分我再截個圖
當Android Studio偵測到有問題的程式碼就會顯示出紅色
移動滑鼠在紅色的附近點擊,會出現下面的畫面
至於要點哪裡,多試幾次就會比較有概念,如果弄不好移動滑鼠的時候畫面也可能會消失,所以滑鼠點下去之後最好是把滑鼠放開,不要移動
此時依照提示按下 Alt + Enter 就會自動載入對應的函式庫
小魚 謝謝<3
前幾天剛創帳號 忘記帳號了導致太慢回應XD
我一直發生"package android.support.v7.app does not exist"的ERROR
好像不能用
import android.support.v7.app.AppCompatActivity;
因為後來我改用
import androidx.appcompat.app.AppCompatActivity;
就沒事了 (?)
版本一直在更新,
以最新測試的為主吧.